﻿using System;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YJKArch.ColumnModule.DataDefine;
using YJKArch.Utils;

namespace YJKArch.ColumnModule
{
	public class IrregularColumnFamilyCreator : ColumnFamilyCreator
	{
		public IrregularColumnFamilyCreator(ExternalCommandData cmdData, ColumnFamily familyInfos, ElementId materialId, Autodesk.Revit.DB.Document familyDocument) : base(cmdData, familyInfos, materialId, familyDocument)
		{
			this.m_FamilyDocument.FamilyManager.RemoveParameter(this.m_Width);
			this.m_FamilyDocument.FamilyManager.RemoveParameter(this.m_Depth);
		}

		public override Result AddReferencePlanes(ref string message)
		{
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public override Result CreateExtrusion(ref string message)
		{
			foreach (CurveArrArray curveArrArray in this.CreateProfileShape())
			{
				SketchPlane sketchPlane = RevitVersionFuncs.NewSketchPlane(this.m_FamilyDocument, this.m_BottomRefPlane.GetRefPlane());
				double num = Common.MMToFeet(4000.0);
				bool flag = true;
				Extrusion extrusion = null;
				try
				{
					extrusion = this.m_FamilyDocument.FamilyCreate.NewExtrusion(flag, curveArrArray, sketchPlane, num);
				}
				catch (Exception)
				{
					continue;
				}
				if (extrusion == null)
				{
					message = "create extrusion failed";
					return Autodesk.Revit.UI.Result.Failed;
				}
				this.m_ExtrusionList.Add(extrusion);
			}
			if (this.m_ExtrusionList.Count < 1)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public override Result AddAlignments(ref string message)
		{
			Face extrusionPlaneFace = base.GetExtrusionPlaneFace(this.m_ExtrusionList[0], this.m_FrontView, XYZ.BasisZ);
			Reference reference = this.m_UpperLevel.PlaneReference();
			this.m_FamilyDocument.FamilyCreate.NewAlignment(this.m_FrontView, reference, extrusionPlaneFace.Reference);
			Face extrusionPlaneFace2 = base.GetExtrusionPlaneFace(this.m_ExtrusionList[0], this.m_FrontView, -XYZ.BasisZ);
			Reference reference2 = this.m_LowerLevel.PlaneReference();
			this.m_FamilyDocument.FamilyCreate.NewAlignment(this.m_FrontView, reference2, extrusionPlaneFace2.Reference);
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public override Result AddParameters(ref string message)
		{
			FamilyManager familyManager = this.m_FamilyDocument.FamilyManager;
			FamilyParameter familyParameter = familyManager.get_Parameter("材质");
			if (familyParameter == null)
			{
				familyParameter = familyManager.AddParameter("材质", BuiltInParameterGroup.PG_MATERIALS, (Autodesk.Revit.DB.ParameterType)9, false);
			}
			foreach (Extrusion extrusion in this.m_ExtrusionList)
			{
				Parameter parameter = extrusion.get_Parameter(BuiltInParameter.MATERIAL_ID_PARAM);
				familyManager.AssociateElementParameterToFamilyParameter(parameter, familyParameter);
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public override Result AddDimensions(ref string message)
		{
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public override Result AddSymbols(ref string message)
		{
			foreach (ColumnSymbol columnSymbol in this.m_FamilyInfos.Symbols)
			{
				string name = columnSymbol.Name;
				DisplayUnit displayUnitSystem = this.m_FamilyDocument.DisplayUnitSystem;
				try
				{
					FamilyManager familyManager = this.m_FamilyDocument.FamilyManager;
					FamilyType currentType = familyManager.NewType(name);
					familyManager.CurrentType = currentType;
				}
				catch (Exception ex)
				{
					message = ex.Message;
					return Autodesk.Revit.UI.Result.Failed;
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
